package com.maoye.ossp.common.web.taglib;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 分页标签java 类
 * 
 * @author yanghao
 * 
 */
public class PageTage extends TagSupport {

	private static final long serialVersionUID = 5729832874890369508L;

	private String url; // 请求URI
	private int pageSize; // 每页要显示的记录数
	private int pageNo; // 当前页号
	private int total; // 总记录数

	public int doStartTag() throws JspException {

		int pageTotal = (total + pageSize - 1) / pageSize; // 计算总页数

		pageTotal = pageTotal == 0 ? 1 : pageTotal;

		StringBuilder sb = new StringBuilder();
		// 页号越界处理
		if (pageNo > pageTotal) {
			pageNo = pageTotal;
		}
		if (pageNo < 1) {
			pageNo = 1;
		}
		sb.append("<input type=\"hidden\" name=\"pageNo\" value=\"")
				.append(pageNo).append("\"/>\r\n");
		sb.append("<input type=\"hidden\" name=\"pageSize\" value=\"")
				.append(pageSize).append("\"/>\r\n");
		sb.append("       <div class=\"Paging row\">\r\n");
		sb.append("             <div class=\"col-xs-6\">\r\n");
		sb.append("                  <ul class=\"pagination\">\r\n");
		// 上一页处理
		if (pageNo == 1) {
			sb.append("                      <li class=\"disabled\"><a href=\"javascript:\">« 上一页</a></li>\r\n");
		} else {
			sb.append("<li><a href=\"javascript:Oosp_TurnOverPage(")
					.append((pageNo - 1))
					.append(")\">&laquo;&nbsp;上一页</a></li>\r\n");
		}
		// 下一页处理
		if (pageNo >= pageTotal) {
			sb.append("                      <li class=\"disabled\"><a href=\"#\">下一页 »</a></li>\r\n");
		} else {
			sb.append(
					"                      <li><a href=\"javascript:Oosp_TurnOverPage(")
					.append((pageNo + 1))
					.append(")\">下一页&nbsp;&raquo;</a></li>\r\n");
		}
		sb.append("                  </ul>\r\n");
		sb.append("             </div>\r\n");
		sb.append("             <div class=\"col-xs-6\">\r\n");
		sb.append("                  <div class=\"pull-right\">\r\n");
		sb.append("                       <span class=\"Paging_2\">第")
				.append(pageNo).append("页</span>&nbsp;&nbsp;\r\n");
		sb.append("                       <span class=\"Paging_2\">共")
				.append(pageTotal).append("页</span>\r\n");
		sb.append("                       <span class=\"Paging_2\">总")
				.append(total).append("条</span>\r\n");
		sb.append("                       <span>到</span><input class=\"form-control_1 input-sm input-goto MaoYe-form-TiaoZhuan\" name=\"gotoPageNo\" type=\"text\">\r\n");
		sb.append("                       <a class=\"btn btn-default btn-sm Paging_3\" href=\"javascript:Oosp_GoTo()\" role=\"button\">跳转</a>\r\n");
		sb.append("                  </div>\r\n");
		sb.append("             </div>\r\n");
		sb.append("        </div> \n");

		// 把生成的HTML输出到响应中
		try {
			pageContext.getOut().println(sb.toString());
		} catch (IOException e) {
			throw new JspException(e);
		}
		return SKIP_BODY;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
}
